package com.xez.util
{
	import com.xez.data.User;
	
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	
	public class DataUtil
	{
		public function DataUtil()
		{
		}
		
		private static var instance:DataUtil;
		
		public static function getInstance():DataUtil
		{
			if (!instance)
				instance = new DataUtil();
			
			return instance;
		}
		
		public function getBase():void
		{
			var hs:HTTPService = new HTTPService();
			hs.resultFormat = 'text';
			hs.addEventListener(ResultEvent.RESULT, getbase_resultHandler);
			hs.addEventListener(FaultEvent.FAULT, getbase_faultHandler);
			hs.url = FsConst.GET_BASE_URL;
			hs.method = 'POST';
			hs.send();
		}
		
		private function getbase_resultHandler(e:ResultEvent):void
		{
			var basexml:XML = new XML(e.result);
			if (basexml)
			{
				FsData.getInstance().newVersion = basexml.version;
				FsData.getInstance().newAppurl = basexml.url;
				var tipMsg:String = basexml.msg;
//				FsData.getInstance().auto = basexml.auto == 'NO' ? false : true;
//				FsData.getInstance().hasUser = basexml.user == 'NO' ? false : true;
				trace('获取版本信息: ', FsData.getInstance().newVersion, 
					FsData.getInstance().newAppurl, 
					FsData.getInstance().auto,
					FsData.getInstance().hasUser);
				
				if (tipMsg != '')
					ViewUtil.tipLong(tipMsg);
				
				if (FsData.getInstance().auto)
					ViewUtil.updateView.cheackUpdate();
			}
		}
		
		private function getbase_faultHandler(e:FaultEvent):void
		{
			trace('获取版本信息失败');
		}
		
		/**
		 * 注册用户
		 * */
		public function userRegister(username:String, password:String):void
		{
			var hs:HTTPService = new HTTPService();
			hs.resultFormat = 'text';
			hs.addEventListener(ResultEvent.RESULT, registerUser_resultHandler);
			hs.addEventListener(FaultEvent.FAULT, registerUser_faultHandler);
			hs.url = FsConst.REGISTER_URL + '?username=' + username + '&password=' + password;
			hs.method = 'POST';
			hs.send();
		}
		
		private function registerUser_resultHandler(e:ResultEvent):void
		{
			if (e.result == 'success')
			{
				ViewUtil.tipShort('用户注册成功');
				ViewUtil.registerView.visible = false;
			}
		}
		
		private function registerUser_faultHandler(e:FaultEvent):void
		{
			ViewUtil.tipShort('用户注册失败');
		}
		
		/**
		 * 用户登录
		 * */
		public function userLogin(username:String, password:String):void
		{
			var hs:HTTPService = new HTTPService();
			hs.resultFormat = 'text';
			hs.addEventListener(ResultEvent.RESULT, userLogin_resultHandler);
			hs.addEventListener(FaultEvent.FAULT, userLogin_faultHandler);
			hs.url = FsConst.LOGIN_URL + '?username=' + username + '&password=' + password;
			hs.method = 'POST';
			hs.send();
		}
		
		private function userLogin_resultHandler(e:ResultEvent):void
		{
			var usersxml:XML = new XML(e.result);
			if (usersxml.user.length() > 0)
			{
				var user:User = new User();
				user.username = usersxml.user[0].username;
				user.password = usersxml.user[0].password;
				user.isvip = usersxml.user[0].isvip == '0' ? false : true;
				user.isboss = usersxml.user[0].isboss == '0' ? false : true;
				user.islogin = true;
				user.setDec();
				
				FsData.getInstance().user = user;
				FsData.getInstance().user.save();
				ViewUtil.tipShort('登录成功');
			}
			else
				ViewUtil.tipShort('登录失败');
		}
		
		private function userLogin_faultHandler(e:FaultEvent):void
		{
			ViewUtil.tipShort('登录失败');
		}
		
	}
}